Learning Perl by Randal L. Schwartz
Author:Randal L. Schwartz
Language: eng
Format: epub, pdf
Publisher: O'Reilly Media
Published: 2016-10-12T00:00:00+00:00
The next Operator
Sometimes you’re not ready for the loop to finish, but you’re done with the current iteration. That’s what the next operator is good for. It jumps to the inside of the bottom of the current loop block. After next, control continues with the next iteration of the loop (much like the continue operator in C or a similar language):
# Analyze words in the input file or files while (<>) { foreach (split) { # break $_ into words, assign each to $_ in turn $total++; next if /\W/; # strange words skip the remainder of the loop $valid++; $count{$_}++; # count each separate word ## next comes here ## } } print "total things = $total, valid words = $valid\n"; foreach $word (sort keys %count) { print "$word was seen $count{$word} times.\n"; }
This one is a little more complex than most of our examples up to this point, so let’s take it step by step. The while loop is reading lines of input from the diamond operator, one after another, into $_; you’ve seen that before. Each time through that loop, another line of input will be in $_.
Inside that loop, the foreach loop iterates over the return value split. Do you remember the default for split with no arguments? That splits $_ on whitespace, in effect breaking $_ into a list of words. Since the foreach loop doesn’t mention some other control variable, the control variable will be $_. So, you’ll see one word after another in $_.
But didn’t we just say that $_ holds one line of input after another? Well, in the outer loop, that’s what it is. But inside the foreach loop, it holds one word after another. It’s no problem for Perl to reuse $_ for a new purpose; this happens all the time.
Now, inside the foreach loop, you’re seeing one word at a time in $_. $total is incremented, so it must be the total number of words. But the next line (which is the point of this example) checks to see whether the word has any nonword characters—anything but letters, digits, and underscores. So, if the word is Tom's, or if it is full-sized, or if it has an adjoining comma, quote mark, or any other strange character, it will match that pattern and you’ll skip the rest of the loop, going on to the next word.
But let’s say that it’s an ordinary word, like fred. In that case, you count $valid up by one, and also $count{$_}, keeping a count for each different word. So, when you finish the two loops, you’ve counted every word in every line of input from every file the user wanted you to use.
We’re not going to explain the last few lines. By now, we hope you’ve got stuff like that down already.
Like last, next may be used in any of the five kinds of loop blocks: for, foreach, while, until, or the naked block. Also, if you nest loop blocks, next works with the innermost one.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7810)
Grails in Action by Glen Smith Peter Ledbrook(7719)
Azure Containers Explained by Wesley Haakman & Richard Hooper(6840)
Configuring Windows Server Hybrid Advanced Services Exam Ref AZ-801 by Chris Gill(6839)
Running Windows Containers on AWS by Marcio Morales(6367)
Kotlin in Action by Dmitry Jemerov(5092)
Microsoft 365 Identity and Services Exam Guide MS-100 by Aaron Guilmette(5070)
Combating Crime on the Dark Web by Nearchos Nearchou(4648)
Microsoft Cybersecurity Architect Exam Ref SC-100 by Dwayne Natwick(4616)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4437)
The Ruby Workshop by Akshat Paul Peter Philips Dániel Szabó and Cheyne Wallace(4335)
The Age of Surveillance Capitalism by Shoshana Zuboff(3979)
Python for Security and Networking - Third Edition by José Manuel Ortega(3895)
The Ultimate Docker Container Book by Schenker Gabriel N.;(3555)
Learn Wireshark by Lisa Bock(3531)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3528)
Mastering Python for Networking and Security by José Manuel Ortega(3376)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3356)
Blockchain Basics by Daniel Drescher(3325)
